/**
 * 时间戳
 * @param {*} timestamp  时间戳
 */
const timestampToTime = (timestamp) => {
    let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M =
        (date.getMonth() + 1 < 10 ?
            '0' + (date.getMonth() + 1) :
            date.getMonth() + 1) + '-'
    let D =
        (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    let h =
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m =
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
        ':'
    let s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return Y + M + D + h + m + s
};
//时间戳转日期不带时分秒
const timestampToDate = (timestamp) => {
    let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M =
        (date.getMonth() + 1 < 10 ?
            '0' + (date.getMonth() + 1) :
            date.getMonth() + 1) + '-'
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
    return Y + M + D;
};
/**
 * 存储localStorage
 */
const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}
/**
 * 获取localStorage
 */
const getStore = name => {
    if (!name) return;
    return window.localStorage.getItem(name);
}
/**
 * 删除localStorage
 */
const removeStore = name => {
    if (!name) return;
    window.localStorage.removeItem(name);
}
/**
 * 设置cookie
 **/
function setCookie(name, value, day) {
    let date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires=' + date;
};
/**
 * 获取cookie
 **/
function getCookie(name) {
    let reg = RegExp(name + '=([^;]+)');
    let arr = document.cookie.match(reg);
    if (arr) {
        return arr[1];
    } else {
        return '';
    }
};
/**
 * 删除cookie
 **/
function delCookie(name) {
    setCookie(name, null, -1);
};

/*加密*/
function encrypt(s){
	let keys = [0xB2, 0x09, 0xBB, 0x55, 0x93, 0x6D,0x44, 0x47];
	let rs = "";
	let j=0,k=0;
	for (let i = 0; i < s.length; i++) {
		k = s.charCodeAt(i)^keys[j];
		k &= 0xFF;
		rs += fix(k.toString(16),2);
		j = (j+1)%8;
	};
	return rs;
}
function fix(num, length) {
	return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
}
//导出数据
function exportData(that,params,model,exportback){
  //说明：fileName参数是导出excel的文件名，operationId和分页列表的operid一样，parameter和分页列表参数一样，多个参数用,隔开
  //title是excel标题
  that.$httpApi.exportByOperid(params).then((res) => {
    if(exportback!=undefined){
      exportback('success');
    }
    let content = res.data;
    let blob = new Blob([content],{type:'application/vnd.ms-excel  application/x-excel;charset=utf-8'});
    let elink = document.createElement('a');
    let href = window.URL.createObjectURL(blob);
    elink.href = href;
    elink.download = model;//导出excel的文件名
    document.body.appendChild(elink);
    elink.click();
    document.body.removeChild(elink);
    window.URL.revokeObjectURL(href);
  }).catch((res)=>{
    if(exportback!=undefined){
      exportback('failed');
    }
  });
}

//日期返回按钮-今天,昨天,一周
function datebtn(){
  let datevalue = {
      /* disabledDate(time) {
        return time.getTime() > Date.now();
      }, */
      shortcuts: [{
        text: '今天',
        onClick(picker) {
          picker.$emit('pick', new Date());
        }
      }, {
        text: '昨天',
        onClick(picker) {
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24);
          picker.$emit('pick', date);
        }
      }, {
        text: '一周前',
        onClick(picker) {
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
          picker.$emit('pick', date);
        }
      }]
  }
  return datevalue;
}
//推算日期前几天,后几天
function dateCalculation(num){
    let date = new Date();
    let afterdate = date.setTime(date.getTime() + (3600 * 1000 * 24*num));
    return timestampToDate(afterdate)
}


Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

//获取当前日期
function getCurrentDate(){
	return (new Date()).Format("yyyy-MM-dd");
}
//获取当前时间
function getCurrentTime(){
	return (new Date()).Format("yyyy-MM-dd hh:mm:ss");
}

function isNULL(o){
	if (typeof (o) == "object"){
		return o == undefined || o == null || o.length == 0;
	};
	if (typeof (o) == "string"){
		return o == undefined || o == null || o.trim() == "" || o.toUpperCase() == "NULL"
	}
	return o == undefined || o == null;
}
/**
 * 导出
 **/
export {
    timestampToTime,
    timestampToDate,
    setStore,
    getStore,
    removeStore,
    setCookie,
    getCookie,
    delCookie,
    encrypt,
    exportData,
    datebtn,
    dateCalculation,
    getCurrentDate,
    getCurrentTime,
    isNULL,
}
